/**   
 * @Title: InitializtionConfigFilter.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.component.util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/** 
 * @Description: 配置初始化的Filter
 * @author: system
 * @date: 2006
 * @version V1.0 
 * 
 */
public class InitializtionConfigFilter implements Filter {
	/** 
	 * @Fields ignore：是否printAccess标准
	 */
	protected boolean ignore = true;
	
	public void destroy() {
		LoaderContext.destroyed();
	}

	
	/**
	 * @Description 拦截器的实现
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		try {
			if(this.ignore)
	           printAccess((HttpServletRequest) request);
			WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(((HttpServletRequest) request).getSession().getServletContext());
			LoaderContext.initialize(context);
			chain.doFilter(request, response);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
		}
	}
	
	/**
	 * @Description 初始化
	 * @param arg0：filterConfig
	 * @throws ServletException
	 */
	public void init(FilterConfig arg0) throws ServletException {
		ConvertUtils.register(new IntegerConverter(null), Integer.class);
		ConvertUtils.register(new LongConverter(null), Long.class);
		ConvertUtils.register(new DoubleConverter(null), Double.class);
		ConvertUtils.register(new FloatConverter(null), Float.class);
		ConvertUtils.register(new DateConvert(),Date.class);
		CopyableUtils.register(new NullCopyable());
		CopyableUtils.register(new CollectionCopyable());
		LoaderContext.initialize(WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()));
	}
	
	/**
	 * @Description 执行拦截器时在console上输出
	 * @param req
	 */
	private void printAccess(HttpServletRequest req) {
        //系统所处路径
		//ConfigConstants.DEFAULT_SYSTEM_DIRECTORY=req.getSession().getServletContext().getRealPath("/");
    	StringBuffer buffer=new StringBuffer(50);
    	buffer.append("本地址 ")
    	.append(req.getRequestURI())
    	.append(" 程序在 ")
    	.append(new Date())
    	.append("时间试图访问本系统");
        System.out.println(buffer.toString());
    }
}
